--- /dev/null
+use std::io::{mod, fs, File};
+
+use flate2::Default;
+use flate2::writer::GzEncoder;
+use git2;
+use serialize::hex::ToHex;
+use tar::Archive;
+use url::Url;
+
+use support::{ResultTest, project};
+use support::paths;
+use support::git::repo;
+use cargo::util::Sha256;
+
+pub fn registry_path() -> Path { paths::root().join("registry") }
+pub fn registry() -> Url { Url::from_file_path(®istry_path()).unwrap() }
+pub fn dl_path() -> Path { paths::root().join("dl") }
+pub fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() }
+
+pub fn init() {
+ let config = paths::root().join(".cargo/config");
+ fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert();
+ File::create(&config).write_str(format!(r#"
+ [registry]
+ index = "{reg}"
+ token = "api-token"
+ "#, reg = registry()).as_slice()).assert();
+
+ // Init a new registry
+ repo(®istry_path())
+ .file("config.json", format!(r#"
+ {{"dl":"{}","api":""}}
+ "#, dl_url()).as_slice())
+ .build();
+}
+
+pub fn mock_archive(name: &str, version: &str, deps: &[(&str, &str)]) {
+ let mut manifest = format!(r#"
+ [package]
+ name = "{}"
+ version = "{}"
+ authors = []
+ "#, name, version);
+ for &(dep, req) in deps.iter() {
+ manifest.push_str(format!(r#"
+ [dependencies.{}]
+ version = "{}"
+ "#, dep, req).as_slice());
+ }
+ let p = project(name)
+ .file("Cargo.toml", manifest.as_slice())
+ .file("src/lib.rs", "");
+ p.build();
+
+ let dst = mock_archive_dst(name, version);
+ fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert();
+ let f = File::create(&dst).unwrap();
+ let a = Archive::new(GzEncoder::new(f, Default));
+ a.append(format!("{}-{}/Cargo.toml", name, version).as_slice(),
+ &mut File::open(&p.root().join("Cargo.toml")).unwrap()).unwrap();
+ a.append(format!("{}-{}/src/lib.rs", name, version).as_slice(),
+ &mut File::open(&p.root().join("src/lib.rs")).unwrap()).unwrap();
+ a.finish().unwrap();
+}
+
+pub fn mock_archive_dst(name: &str, version: &str) -> Path {
+ dl_path().join(name).join(version).join("download")
+}
+
+pub fn mock_pkg(name: &str, version: &str, deps: &[(&str, &str)]) {
+ mock_archive(name, version, deps);
+ let c = File::open(&mock_archive_dst(name, version)).read_to_end().unwrap();
+ let line = pkg(name, version, deps, cksum(c.as_slice()).as_slice());
+
+ let file = match name.len() {
+ 1 => format!("1/{}", name),
+ 2 => format!("2/{}", name),
+ 3 => format!("3/{}/{}", name.slice_to(1), name),
+ _ => format!("{}/{}/{}", name.slice(0, 2), name.slice(2, 4), name),
+ };
+ publish(file.as_slice(), line.as_slice());
+}
+
+pub fn publish(file: &str, line: &str) {
+ let repo = git2::Repository::open(®istry_path()).unwrap();
+ let mut index = repo.index().unwrap();
+ {
+ let dst = registry_path().join(file);
+ let prev = File::open(&dst).read_to_string().unwrap_or(String::new());
+ fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).unwrap();
+ File::create(&dst).write_str((prev + line + "\n").as_slice()).unwrap();
+ }
+ index.add_path(&Path::new(file)).unwrap();
+ index.write().unwrap();
+ let id = index.write_tree().unwrap();
+ let tree = repo.find_tree(id).unwrap();
+ let sig = repo.signature().unwrap();
+ let parent = repo.refname_to_id("refs/heads/master").unwrap();
+ let parent = repo.find_commit(parent).unwrap();
+ repo.commit(Some("HEAD"), &sig, &sig,
+ "Another commit", &tree,
+ [&parent]).unwrap();
+}
+
+pub fn pkg(name: &str, vers: &str, deps: &[(&str, &str)], cksum: &str) -> String {
+ let deps = deps.iter().map(|&(a, b)| dep(a, b)).collect::<Vec<String>>();
+ format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#,
+ name, vers, deps, cksum)
+}
+
+pub fn dep(name: &str, req: &str) -> String {
+ format!("{{\"name\":\"{}\",\
+ \"req\":\"{}\",\
+ \"features\":[],\
+ \"default_features\":false,\
+ \"target\":null,\
+ \"optional\":false}}", name, req)
+}
+
+pub fn cksum(s: &[u8]) -> String {
+ let mut sha = Sha256::new();
+ sha.update(s);
+ sha.finish().to_hex()
+}
-use std::io::{mod, fs, File};
-use url::Url;
-use git2;
-use serialize::hex::ToHex;
+use std::io::File;
-use support::{ResultTest, project, execs, cargo_dir};
+use support::{project, execs, cargo_dir};
use support::{UPDATING, DOWNLOADING, COMPILING, PACKAGING, VERIFYING};
-use support::paths;
-use support::git::repo;
-use cargo::util::Sha256;
+use support::registry as r;
use hamcrest::assert_that;
-fn registry_path() -> Path { paths::root().join("registry") }
-fn registry() -> Url { Url::from_file_path(®istry_path()).unwrap() }
-fn dl_path() -> Path { paths::root().join("dl") }
-fn dl_url() -> Url { Url::from_file_path(&dl_path()).unwrap() }
-
-fn cksum(s: &[u8]) -> String {
- let mut sha = Sha256::new();
- sha.update(s);
- sha.finish().to_hex()
-}
-
fn setup() {
- let config = paths::root().join(".cargo/config");
- fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert();
- File::create(&config).write_str(format!(r#"
- [registry]
- index = "{reg}"
- token = "api-token"
- "#, reg = registry()).as_slice()).assert();
-
- // Prepare the "to download" artifacts
- let foo = include_bin!("fixtures/foo-0.0.1.tar.gz");
- let bar = include_bin!("fixtures/bar-0.0.1.tar.gz");
- let notyet = include_bin!("fixtures/notyet-0.0.1.tar.gz");
- let foo_cksum = dl("foo", "0.0.1", foo);
- let bar_cksum = dl("bar", "0.0.1", bar);
- dl("bad-cksum", "0.0.1", foo);
- let notyet = dl("notyet", "0.0.1", notyet);
-
- // Init a new registry
- repo(®istry_path())
- .file("config.json", format!(r#"
- {{"dl":"{}","api":""}}
- "#, dl_url()).as_slice())
- .file("3/f/foo", pkg("foo", "0.0.1", [], &foo_cksum))
- .file("3/b/bar", pkg("bar", "0.0.1", [
- "{\"name\":\"foo\",\
- \"req\":\">=0.0.0\",\
- \"features\":[],\
- \"default_features\":false,\
- \"target\":null,\
- \"optional\":false}"
- ], &bar_cksum))
- .file("ba/d-/bad-cksum", pkg("bad-cksum", "0.0.1", [], &bar_cksum))
- .nocommit_file("no/ty/notyet", pkg("notyet", "0.0.1", [], ¬yet))
- .build();
-
- fn pkg(name: &str, vers: &str, deps: &[&str], cksum: &String) -> String {
- format!(r#"{{"name":"{}","vers":"{}","deps":{},"cksum":"{}","features":{{}}}}"#,
- name, vers, deps, cksum)
- }
- fn dl(name: &str, vers: &str, contents: &[u8]) -> String {
- let dst = dl_path().join(name).join(vers).join("download");
- fs::mkdir_recursive(&dst.dir_path(), io::USER_DIR).assert();
- File::create(&dst).write(contents).unwrap();
- cksum(contents)
- }
-}
-
-fn publish_notyet() {
- let repo = git2::Repository::open(®istry_path()).unwrap();
- let mut index = repo.index().unwrap();
- index.add_path(&Path::new("no/ty/notyet")).unwrap();
- let id = index.write_tree().unwrap();
- let tree = repo.find_tree(id).unwrap();
- let sig = repo.signature().unwrap();
- let parent = repo.refname_to_id("refs/heads/master").unwrap();
- let parent = repo.find_commit(parent).unwrap();
- repo.commit(Some("HEAD"), &sig, &sig,
- "Another commit", &tree,
- [&parent]).unwrap();
+ r::init();
}
test!(simple {
authors = []
[dependencies]
- foo = ">= 0.0.0"
+ bar = ">= 0.0.0"
"#)
.file("src/main.rs", "fn main() {}");
+ r::mock_pkg("bar", "0.0.1", []);
+
assert_that(p.cargo_process("build"),
execs().with_status(0).with_stdout(format!("\
{updating} registry `{reg}`
-{downloading} foo v0.0.1 (the package registry)
-{compiling} foo v0.0.1 (the package registry)
+{downloading} bar v0.0.1 (the package registry)
+{compiling} bar v0.0.1 (the package registry)
{compiling} foo v0.0.1 ({dir})
",
updating = UPDATING,
downloading = DOWNLOADING,
compiling = COMPILING,
dir = p.url(),
- reg = registry()).as_slice()));
+ reg = r::registry()).as_slice()));
// Don't download a second time
assert_that(p.cargo_process("build"),
execs().with_status(0).with_stdout(format!("\
{updating} registry `{reg}`
-[..] foo v0.0.1 (the package registry)
+[..] bar v0.0.1 (the package registry)
[..] foo v0.0.1 ({dir})
",
updating = UPDATING,
dir = p.url(),
- reg = registry()).as_slice()));
+ reg = r::registry()).as_slice()));
})
test!(deps {
"#)
.file("src/main.rs", "fn main() {}");
+ r::mock_pkg("baz", "0.0.1", []);
+ r::mock_pkg("bar", "0.0.1", [("baz", "*")]);
+
assert_that(p.cargo_process("build"),
execs().with_status(0).with_stdout(format!("\
{updating} registry `{reg}`
{downloading} [..] v0.0.1 (the package registry)
{downloading} [..] v0.0.1 (the package registry)
-{compiling} foo v0.0.1 (the package registry)
+{compiling} baz v0.0.1 (the package registry)
{compiling} bar v0.0.1 (the package registry)
{compiling} foo v0.0.1 ({dir})
",
downloading = DOWNLOADING,
compiling = COMPILING,
dir = p.url(),
- reg = registry()).as_slice()));
+ reg = r::registry()).as_slice()));
})
test!(nonexistent {
"#)
.file("src/main.rs", "fn main() {}");
+ r::mock_pkg("bad-cksum", "0.0.1", []);
+ File::create(&r::mock_archive_dst("bad-cksum", "0.0.1")).unwrap();
+
assert_that(p.cargo_process("build").arg("-v"),
execs().with_status(101).with_stderr("\
Unable to get packages from source
version required: >= 0.0.0
"));
- publish_notyet();
+ r::mock_pkg("notyet", "0.0.1", []);
assert_that(p.process(cargo_dir().join("cargo")).arg("build"),
execs().with_status(0).with_stdout(format!("\
downloading = DOWNLOADING,
compiling = COMPILING,
dir = p.url(),
- reg = registry()).as_slice()));
+ reg = r::registry()).as_slice()));
})
test!(package_with_path_deps {
version required: ^0.0.1
"));
- publish_notyet();
+ r::mock_pkg("notyet", "0.0.1", []);
assert_that(p.process(cargo_dir().join("cargo")).arg("package"),
execs().with_status(0).with_stdout(format!("\